<template>
	<!-- 项目管理 -->
	<view>
		<u-tabs :list="tabsList" active-color="#849ee3" inactive-color="#606266" :is-scroll="false" :current="current"
		 @change="change"></u-tabs>

		<view class="container u-relative" style="top: 78rpx;">
			<template v-if="projectList.length > 0">
				<block v-for="(item,index) in projectList" :key="index">
					<view class="project-item u-padding-20 u-flex u-margin-bottom-20 white-bg z-b-r">
						<view class="item-left u-flex-1" @click="projectDetail(item._id,item.salary_way)">
							<view class="u-margin-bottom-10">
								<text>项目名称：</text>
								<text>{{item.project_name}}</text>
							</view>
							<view class="u-margin-bottom-10">
								<text>项目地址：</text>
								<text>{{item.project_address}}</text>
							</view>
							<view class="u-margin-bottom-10">
								<text>项目甲方：</text>
								<text>{{item.project_jia_info}}</text>
							</view>
						</view>
						<view class="u-margin-right-10 u-margin-left-20" @tap="taptext($event,item._id,index)" style="color: #2979ff;">
							<u-icon name="more-dot-fill"></u-icon>更多
						</view>
					</view>
					<chunLei-popups v-model="showPopups" :popData="popupSdata" @tapPopup="tapPopupHandle" :x="popupsX" :y="popupsY" placement="top-end">
					</chunLei-popups>
				</block>
			</template>
			<template v-else>
				<u-empty text="暂无项目数据" mode="data"></u-empty>
			</template>
		</view>
		
		<!-- 模态框 -->
		<u-modal v-model="showModal" show-cancel-button="true" @confirm="confirmDelProject" :content="content"></u-modal>
		<u-top-tips ref="uTips"></u-top-tips>
	</view>
</template>

<script>
	import {mapState} from "vuex";
	import chunLeiPopups from "@/components/chunLei-popups/chunLei-popups.vue";
	export default {
		components:{chunLeiPopups},
		data() {
			return {
				popupSdata:[{title:'编辑',id:'edit'},{title:'删除',id:'del'}],
				showPopups:false,
				popupsX:0,
				popupsY:0,
				showModal:false,
				content:"删除后将会清空项目数据，确定删除吗？",
				tabsList: [{
					name: '进行中',
					id:2
				}, {
					name: '待启动',
					id:1
				}, {
					name: '已结束',
					id:3
				}],
				current: 0,
				projectList:[],
				curProjectID:"",
				curProjectIndex:0,
			}
		},
		computed:{
			...mapState({
				userID:"userID",
				token:"token"
			})
		},
		onLoad() {
			let statusID = this.tabsList[this.current].id;
			this.getProjectInfo(statusID)
		},
		methods: {
			tapPopupHandle(e){
				if(e.id === 'del'){ //删除
					this.showModal = true;
				} else if(e.id === 'edit'){ //编辑
					uni.navigateTo({
						url:`../create-project/create-project?_id=${this.curProjectID}`
					})
				}
			},
			async confirmDelProject(){
				this.$loading("正在删除");
				let {curProjectID,curProjectIndex,token} = this;
				let res = await this.$network({
					api:"main",
					data:{
						action:"projectAdmin/deleteProject",
						data:{id:curProjectID,token}
					}
				});
				
				uni.hideLoading()
				if(res.status === 0){
					this.projectList.splice(curProjectIndex,1);
					this.$refs.uTips.show({
						title: '项目已经删除！',
						type: 'warning'
					})
				}
			},
			taptext(e,projectID,index){
				this.curProjectIndex = index;
				this.curProjectID = projectID;
				this.popupsX = e.touches[0].clientX;
				this.popupsY = e.touches[0].clientY;
				this.showPopups = !this.showPopups;
			},
			change(index) {
				this.current = index;
				let statusID = this.tabsList[this.current].id;
				this.getProjectInfo(statusID)
			},
			async getProjectInfo(statusID){
				this.$loading("正在加载");
				this.projectList = [];
				let {userID} = this;
				let res = await this.$network({
					api:"main",
					data:{
						action:"projectAdmin/getProjectInfo",
						data:{statusID,userID}
					}
				});
				
				uni.hideLoading()
				if(res.status === 0){
					this.projectList = res.data;
				}
			},
			async projectDetail(id,salary_way){
				uni.navigateTo({url:`../project-detail/project-detail?projectID=${id}&salaryWay=${salary_way}`})
			}
		}
	}
</script>

<style>
	
</style>
